<?php
require_once 'aws_signed_request.php';

class AmazonProductAPI {

    private $associate_tag;
    private $public_key;
    private $private_key;

    public function setAssociateTag($associate_tag) {
        $this->associate_tag = $associate_tag;
    }

    public function setPublicKey($public_key) {
        $this->public_key = $public_key;
    }

    public function setPrivateKey($private_key) {
        $this->private_key = $private_key;
    }

    private function verifyXmlResponse($response) {
        if ($response === False) {
            throw new Exception("Could not connect to Amazon");
        }
        else {
            if (isset($response->Items->Item->ItemAttributes->Title)) {
                return ($response);
            }
            else {
                throw new Exception("Invalid xml response.");
            }
        }
    }

    private function queryAmazon($parameters) {
        return aws_signed_request("com", $parameters, $this->public_key, $this->private_key);
    }

    public function getItemByAsin($asin_code) {
        $parameters = array("Operation"     => "ItemLookup",
                "ItemId"        => $asin_code,
                "AssociateTag" => $this->associate_tag ,
                "ReviewSort"=>"-HelpfulVotes",
                "ResponseGroup"=>"Large,OfferFull,Offers,OfferSummary,Reviews,Similarities,Variations");

        $xml_response = $this->queryAmazon($parameters);

        return $this->verifyXmlResponse($xml_response);
    }

    public function getItemByKeyword($search , $pages = 1) {

        $parameters = array(
                "Operation"=>"ItemSearch",
                "AssociateTag"=>$this->associate_tag,
                "Keywords"=>$search,
                "SearchIndex"=>"All",
                "ItemPage"=>$pages,
                'sort' => '+pmrank',
                "ReviewSort"=>"-HelpfulVotes",
                "ResponseGroup"=>"Large,OfferFull,Offers,OfferSummary,Reviews,Similarities,Variations");

        $xml_response = $this->queryAmazon($parameters);

        return $this->verifyXmlResponse($xml_response);

    }

    public function searchProducts($search, $category, $searchType = "UPC" , $pages = 1 ) {
        $allowedTypes = array("UPC", "TITLE", "ARTIST", "KEYWORD");
        $allowedCategories = array("Music", "DVD", "VideoGames");

        if($pages == 0)
            $pages = 1;

        switch($searchType) {
            case "UPC" :    $parameters = array("Operation"     => "ItemLookup",
                        "ItemId"        => $search,
                        "SearchIndex"   => $category,
                        "IdType"        => "UPC",
                        "ResponseGroup" => "Medium");
                break;

            case "TITLE" :  $parameters = array("Operation"     => "ItemSearch",
                        "Keywords"         => $search,
                        "SearchIndex"   => $category,
                        "AssociateTag" => $this->associate_tag ,
                        "ItemPage" => $pages ,
                        'sort' => '+pmrank',
                        "ResponseGroup" => "Medium");
                break;

        }

        $xml_response = $this->queryAmazon($parameters);

        return $this->verifyXmlResponse($xml_response);

    }

}

?>
